package com.jzxiang.pickerview.sample

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.jzxiang.pickerview.TimePickerDialog
import com.jzxiang.pickerview.data.Type
import com.jzxiang.pickerview.listener.OnDateSetListener
import com.jzxiang.pickerview.sample.ui.theme.TimePickerDialogXTheme
import java.text.SimpleDateFormat
import java.util.Date


class MainActivity : AppCompatActivity(), OnDateSetListener {
    private val tenYears by lazy {
        10L * 365 * 1000 * 60 * 60 * 24L
    }
    private val mDialogYearMonth by lazy {
        TimePickerDialog.Builder()
            .setType(Type.YEAR_MONTH)
            .setThemeColor(resources.getColor(R.color.colorPrimary))
            .setCallBack(this)
            .build()
    }
    private val mDialogAll by lazy {
        TimePickerDialog.Builder()
            .setCallBack(this)
            .setCancelStringId("Cancel")
            .setSureStringId("Sure")
            .setTitleStringId("TimePicker")
            .setYearText("Year")
            .setMonthText("Month")
            .setDayText("Day")
            .setHourText("Hour")
            .setMinuteText("Minute")
            .setCyclic(false)
            .setMinMillseconds(System.currentTimeMillis())
            .setMaxMillseconds(System.currentTimeMillis() + tenYears)
            .setCurrentMillseconds(System.currentTimeMillis())
            .setThemeColor(resources.getColor(com.jzxiang.pickerview.R.color.timepicker_dialog_bg))
            .setType(Type.ALL)
            .setWheelItemTextNormalColor(resources.getColor(com.jzxiang.pickerview.R.color.timetimepicker_default_text_color))
            .setWheelItemTextSelectorColor(resources.getColor(com.jzxiang.pickerview.R.color.timepicker_toolbar_bg))
            .setWheelItemTextSize(12)
            .build()
    }
    private val mDialogYearMonthDay by lazy {
        TimePickerDialog.Builder()
            .setTitleStringId("请选择")
            .setType(Type.YEAR_MONTH_DAY)
            .setCallBack(this)
            .build()
    }
    private val mDialogMonthDayHourMinute by lazy {
        TimePickerDialog.Builder()
            .setType(Type.MONTH_DAY_HOUR_MIN)
            .setCallBack(this)
            .build()
    }
    private val mDialogHourMinute by lazy {
        TimePickerDialog.Builder()
            .setType(Type.HOURS_MINS)
            .setCallBack(this)
            .build()
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TimePickerDialogXTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Column {
                        TextButton(onClick = {
                            mDialogAll.show(supportFragmentManager,"all")
                        }) {
                            Text(text = "年月日时分秒")
                        }
                        TextButton(onClick = {
                            mDialogYearMonth.show(supportFragmentManager,"year_month")
                        }) {
                            Text(text = "年月")
                        }
                        TextButton(onClick = {
                            mDialogYearMonthDay.show(supportFragmentManager,"year_month_day")
                        }) {
                            Text(text = "年月日")
                        }
                        TextButton(onClick = {
                            mDialogMonthDayHourMinute.show(supportFragmentManager,"month_day_hour_minute")
                        }) {
                            Text(text = "月日时分")
                        }
                        TextButton(onClick = {
                            mDialogHourMinute.show(supportFragmentManager,"hour_minute")
                        }) {
                            Text(text = "时分")
                        }
                    }
                }
            }
        }
    }

    override fun onDateSet(timePickerView: TimePickerDialog?, millseconds: Long) {
        Toast.makeText(this, getDateToString(millseconds), Toast.LENGTH_SHORT).show()
    }

    @SuppressLint("SimpleDateFormat")
    fun getDateToString(time: Long): String? {
        val d = Date(time)
        return SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d)
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    TimePickerDialogXTheme {
        Greeting("Android")
    }
}